﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TripsLogic.controller;
using TripsLogic.util;
using TripsLogic.WeSStatus;

namespace TripsLogic
{
    public partial class forget_password : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                loadLang();
               
            }
        }

        private void loadLang()
        {
            this.litEmail.Text = LangController.getLng("litEmail.Text", "Email");
            this.litForgetPassword.Text = LangController.getLng("litForgetPassword.Text", "Forget password");
            this.Title = LangController.getLng("litWebsiteTitle.Text", "TripsLogic") + " - " + this.litForgetPassword.Text;
            this.lbtSend.Text = LangController.getLng("litSend.Text", "Send");
            this.reqValEmail.ErrorMessage = LangController.getLng("litRequired.Text", "(*)");
            this.rgeValEmail.ErrorMessage = LangController.getLng("litInvalidFormat.Text", "Invalid format");
        }

        protected void lbtSend_Click(object sender, EventArgs e)
        {
            WeSSysUser.CheckAccountResult result = new WeSSysUser.WeSSysUserClient().checkAccount(txtEmail.Text);
            if (!(result.sysUserResultStatus.statusCode == CheckAccountResult.STATUS_UNAVAILABLE_CODE))
            {
                this.lblEmailResponse.ForeColor = System.Drawing.Color.Red;
                this.lblEmailResponse.Text = LangController.getLng("litNonExistAccount.Text", "Non-existed account.");
            }
            else
            {
                forgetPasswordResponse.Visible = true;
                contactform.Visible = false;

                WeSSysUser.ResetPasswordResult result2 = new WeSSysUser.WeSSysUserClient().resetPassword(txtEmail.Text);
                if (result2.sysUserResultStatus.statusCode == 0 && result2.password != "")
                {
                    bool sendResult = sendResetPasswordEmail(txtEmail.Text, result2.password);
                    if (sendResult)
                    {
                        this.litForgetPasswordResponse.Text = String.Format(LangMsgController.getLngMsg("litForgetPasswordResponse.OK", "An email contains new password has been sent to <font style='font-weight:bold'><u>{0}</u></font>. Please check your inbox and log in by using new password."), txtEmail.Text);
                        this.Title = LangController.getLng("litWebsiteTitle.Text", "TripsLogic") + " - " + LangController.getLng("litForgetPasswordResponse.OK", "New password has been generated successfully.");
                    }
                    else
                    {
                        this.litForgetPasswordResponse.Text = LangMsgController.getLngMsg("litForgetPasswordResponse.Error", "Error occurs in generating new password progress.<br/>Please try again later.");
                        this.Title = LangController.getLng("litWebsiteTitle.Text", "TripsLogic") + " - " + LangController.getLng("litForgetPasswordResponse.Error", "Error occurs in reseting password progress");
                    }
                }
                else
                {
                    this.litForgetPasswordResponse.Text = LangMsgController.getLngMsg("litForgetPasswordResponse.Error", "Error occurs in generating new password progress.<br/>Please try again later.");
                    this.Title = LangController.getLng("litWebsiteTitle.Text", "TripsLogic") + " - " + LangController.getLng("litForgetPasswordResponse.Error", "Error occurs in reseting password progress");
                }
            }
        }

        private bool sendResetPasswordEmail(string email, string randomPassword)
        {
            try
            {
                string resetPasswordEmailTemplate = TemplateController.getTemplate("EMAIL_RESETPASSWORD_TEMP", "<p><font style='font-weight:bold'>TripsLogic</font> - Your new password information</p>" +
                        "<p><strong>Email:</strong> {0}</p>" +
                        "<p><strong>New password:</strong> {1}</p>" +
                        "<p>This message has been sent from <font style='font-weight:bold'>TripsLogic</font></p>" +
                        "<p><font style='font-weight:bold'>TripsLogic Team</font></p>");
                Common.sendEmail(txtEmail.Text, "", "", "", LangController.getLng("litResetPasswordEmail.Subject", "[TripsLogic] New password information"),
                    String.Format(resetPasswordEmailTemplate, email, randomPassword), "");

                return true;
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error sendResetPasswordEmail "));
                return false;
            }
        }
    }
}